﻿namespace Leetcode.N0780;
public class Solution
{
    public bool ReachingPoints(int sx, int sy, int tx, int ty)
    {
        var s = sx + sy;
        while ((sx <= tx || sy <= ty) && (sx != tx || sy != ty))
        {
            if (!this.Change(sx, sy, ref tx, ref ty))
            {
                return sx == tx && sy == ty;
            }
        }

        return (sx == tx && sy == ty);
    }

    private bool Change(int sx, int sy, ref int tx, ref int ty)
    {
        if (tx > ty)
        {
            tx = tx - ty;
            if (tx > sx && tx > ty)
            {
                tx -= ((tx - sx) / ty) * ty;
            }

            return true;
        }
        else if (ty > tx)
        {
            ty = ty - tx;
            if (ty > sy && ty > tx)
            {
                ty -= ((ty - sy) / ty) * tx;
            }
            return true;
        }
        return false;
    }
}
